<?PHP
/*
 * $Id: barge-flashlite.classes.php 139 2009-12-16 15:29:56Z namba0219 $
 * =============================================================================
 * Barge - Web application framework on PHP5
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
namespace barge\flashlite;

use barge\utils\HashMap;

/**
 * Flash Lite
 * @see http://libpanda.s18.xrea.com/test_insert/swfmake.php.txt
 */
class SWFInjector
{
	private function h32($sizeint)
	{
		return pack("V",$sizeint);
	}
	private function h16($sizeint)
	{
		return pack("v",$sizeint);
	}

	/**
	 * @param HashMap $dataarray
	 */
	private function calctaglen(HashMap $dataarray)
	{
		$ret = 0;
		foreach($dataarray as $key => $value){
			$ret += strlen($key)+strlen($value) + 11;
		}
		return $ret+1;
	}
	
	/**
	 * @param HashMap $dataarray
	 */
	private function maketag(HashMap $dataarray)
	{
		$tag = "\x3f\x03";
		$taglen = $this->calctaglen($dataarray);
		$tag .= $this->h32($taglen);

		foreach($dataarray as $key => $value){
			$tag .= "\x96".$this->h16(strlen($key)+2)."\x00".$key."\x00";
			$tag .= "\x96".$this->h16(strlen($value)+2)."\x00".$value."\x00";
			$tag .= "\x1d";
		}
		$tag .= "\x00";
		return $tag;
	}

	/**
	 * @param string $srcswf
	 * @param HashMap $parameters
	 */
	public function inject($srcswf, $distswf, HashMap $parameters) 
	{
		$doactiontag = $this->maketag($parameters);

		$fr = fopen($srcswf,"rb");

		$headtmp = fread($fr,9);
		$rb = ord(substr($headtmp,8,1))>>3; // rectbit
		$headlen = ceil(((( 8 - (($rb*4+5)&7) )&7)+ $rb*4 + 5 )/8) + 12 + 5;
		$head = $headtmp.fread($fr,$headlen-9);
	
		$oldsize = filesize($srcswf);
		$newsize = $oldsize+strlen($doactiontag);
		$newhead = substr($head,0,4).h32($newsize).substr($head,8);
	
		$tail = fread($fr, $oldsize-$headlen);
		fclose($fr);

		header("Content-Type: application/x-shockwave-flash");
		print $newhead.$doactiontag.$tail;
	
		/* ƒtƒ@ƒCƒ‹‚É‘‚«‚¾‚·ê‡
		 $destswf = "out.swf";
		 $fw = fopen($destswf,"wb");
		 fwrite($fw,$newhead);
		 fwrite($fw,$doactiontag);
		 fwrite($fw,$tail);
		 fclose($fw);
		 print "<html><head></head><body><a href=\"$destswf\">$destswf</a></body>"
		 */
	}
}

